package com.platform.web.controller.report;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.platform.base.domain.BaseFactory;
import com.platform.base.domain.MeterInfo;
import com.platform.base.domain.ValveInfo;
import com.platform.base.domain.ValveType;
import com.platform.base.service.*;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.data.domain.ValveData;
import com.platform.data.mapper.ValveDataMapper;
import com.platform.data.service.IValveDataService;
import com.platform.data.service.IValveReadService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 阀门统计曲线图
 *
 * @author wangqi
 * @date 2020-08-04
 */
@Controller
@RequestMapping("/report/valve/valvetemp")
public class ValveTempReportController extends BaseController {

    private String prefix = "report/valve/valvetemp";

    @Autowired
    private IValveDataService valveDataService;

    @Autowired
    private IValveReadService valveReadService;
    @Autowired
    private ValveDataMapper valveDataMapper;
    @Autowired
    private IValveTypeService valveTypeService;

    @Autowired
    private IValveInfoService valveInfoService;

    @Autowired
    private IMeterTypeService meterTypeService;

    @Autowired
    private IMeterInfoService meterInfoService;

    @Autowired
    private ICollectorTypeService collectorTypeService;

    @Autowired
    private ICollectorInfoService collectorInfoService;

    @Autowired
    private RedisUtil redisUtil;

    @RequiresPermissions("report:valve:valvetemp:view")
    @GetMapping
    public String valve( ModelMap mmap)
    {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        ca.add(Calendar.MONTH, -1);
        Date resultDate = ca.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        mmap.put("beginTime", dateFormat.format(resultDate));
        mmap.put("endTime", dateFormat.format(new Date()));


        return prefix + "/valve";
    }

    /**
     * 室温变化曲线图
     */
    @PostMapping("/valveSummary")
    @ResponseBody
    public AjaxResult valveSummary(ValveData valveData)
    {
        if(valveData.getParams().get("beginTime").equals("") && valveData.getParams().get("endTime").equals("")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            valveData.getParams().put("beginTime", simpleDateFormat.format(new Date()));
            valveData.getParams().put("endTime", simpleDateFormat.format(new Date()));
        }
        if(valveData.getValveId() != null && !valveData.getValveId().equals("")){
            valveData.setLocationId(null);
        }
        List<ValveData> valveDataList  = valveDataService.selectValveDatarList(valveData);
        return AjaxResult.success(valveDataList);
    }


    /**
     * 阀门类型统计
     */
    @PostMapping("/valveTypeSummary")
    @ResponseBody
    public AjaxResult valveTypeSummary()
    {
        List<ValveType> valveTypeList  = valveTypeService.list();
        for(ValveType valveType : valveTypeList){
            int count = valveInfoService.count(new QueryWrapper<ValveInfo>().eq("type_id", valveType.getTypeId()));
            valveType.setCount(count);
        }
        return AjaxResult.success(valveTypeList);
    }
    /**
     * 阀门设置温度
     */
    @PostMapping("/valveTempSummary")
    @ResponseBody
    public AjaxResult valveTempSummary(ValveData valveData)
    {


        List<ValveData> valveDataList = new ArrayList<ValveData>();
        //List<ValveData> valveDataList = valveDataService.selectValveDataList(valveData);

         valveData = new ValveData();
        valveData.setTemps(10);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "10")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(10.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "10.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(11);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "11")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(11.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "11.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(12);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "12")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(12.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "12.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(13);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "13")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(13.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "13.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(14);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "14")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(14.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "14.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(15);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "15")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(15.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "15.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(16);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "16")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(16.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "16.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(17);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "17")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(17.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "17.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(18);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "18")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(18.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "18.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(19);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "19")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(19.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "19.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(20);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "20")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(20.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "20.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(21);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "21")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(21.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "21.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(22);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "22")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(22.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "22.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(23);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "23")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(23.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "23.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(24);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "24")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(24.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "24.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(25);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "25")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(25.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "25.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(26);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "26")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(26.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "26.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(27);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "27")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(27.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "27.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(28);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "28")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(28.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "28.5")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(29);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "29")));
        valveDataList.add(valveData);

         valveData = new ValveData();
        valveData.setTemps(29.5);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "29.5")));
        valveDataList.add(valveData);
         valveData = new ValveData();
        valveData.setTemps(30);
        valveData.setTempCount(valveDataService.count(new QueryWrapper<ValveData>().eq("temp", "30")));
        valveDataList.add(valveData);




        return AjaxResult.success(valveDataList);
    }
    /**
     * 热表数据统计
     */
    @PostMapping("/meterInfoSummary")
    @ResponseBody
    public AjaxResult meterInfoSummary()
    {
        List<MeterInfo> meterInfoList = new ArrayList<MeterInfo>();

        MeterInfo info = new MeterInfo();
        info.setTypeName("未登记热表");
        info.setCount(meterInfoService.count(new QueryWrapper<MeterInfo>().eq("register_flag", "0")));
        meterInfoList.add(info);

        info = new MeterInfo();
        info.setTypeName("已登记热表");
        info.setCount(meterInfoService.count(new QueryWrapper<MeterInfo>().eq("register_flag", "1")));
        meterInfoList.add(info);

        return AjaxResult.success(meterInfoList);
    }
}
